package com.example.edwincompose.component

import android.util.Log
import android.widget.Space
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.edwincompose.core.extension.noRippleClickable
import com.example.edwincompose.feature.main.TopLevelDestination
import kotlinx.coroutines.launch

/**
 * 底部导航
 */
@Composable
fun MyNavigationBar(
    destinations: List<TopLevelDestination>,
    currentDestination: String,
    onNavigateToDestination: (Int) -> Unit,
    modifier: Modifier = Modifier,
): Unit {
    //如果让row的内容水平居中呢？？？
    Row(
        modifier = modifier
            .fillMaxWidth()
            .navigationBarsPadding()//因为全面屏会遮住，所以要增加这个安全区。
    ) {
        destinations.forEachIndexed { index, destination ->
            val selected = destination.route == currentDestination
            Log.d("wenlong", "MyNavigationBar: "+destination.route+" "+currentDestination)
            val color = if (selected) {
                Color.Blue
            } else {
                Color.Black
            }

            Column(
                horizontalAlignment = Alignment.CenterHorizontally,
                modifier = Modifier
                    .weight(1f)
                    .padding(vertical = 5.dp)
                    .noRippleClickable {
                        onNavigateToDestination(index)
                    }
            ) {
                Image(
                    painter = painterResource(
                        id =
                            if (selected) {
                                destination.selectedIcon
                            } else {
                                destination.unselectedIcon
                            }
                    ),
                    contentDescription = null,
                    modifier = Modifier.size(25.dp)
                )
                Spacer(modifier = Modifier.height(5.dp))
                Text(
                    text = stringResource(id = destination.titleTextId),
                    color = color

                )
            }
        }
    }
}

@Preview
@Composable
fun navigationBarPreview(){
    MyNavigationBar(
        destinations = TopLevelDestination.entries,
        currentDestination = "",
        onNavigateToDestination = {index->

        },
        modifier = Modifier
    )
}